package com.zhiyun.service.impl;

import com.zhiyun.domain.TestPaper;
import com.zhiyun.domain.TestQuestion;
import com.zhiyun.mapper.TestPaperMapper;
import com.zhiyun.mapper.TestQuestionMapper;
import com.zhiyun.service.TestQuestionService;
import com.zhiyun.util.Db;
import com.zhiyun.vo.Result;

import java.util.List;

/**
 * @author zc
 * @Date 2025/5/6 上午8:17
 */
public class TestQuestionServiceImpl implements TestQuestionService {
    TestQuestionMapper testQuestionMapper = Db.getMapper(TestQuestionMapper.class);
    TestPaperMapper testPaperMapper = Db.getMapper(TestPaperMapper.class);
    @Override
    public Result selectByPaperId(int testPaperId) {
        List<TestQuestion> testQuestions = testQuestionMapper.selectByPaperIdWithQuestionType(testPaperId);
        return Result.ok(testQuestions);
    }

    @Override
    public Result update(TestQuestion testQuestion) {
        try {
            Db.startTransaction();
            TestQuestion oldTestQuestion = testQuestionMapper.selectByPrimaryKey(testQuestion.getId());
            if (oldTestQuestion == null) {
                return Result.fail("该题目不存在");
            }
            //先修改题目
            int rows = testQuestionMapper.updateByPrimaryKey(testQuestion);
            //重新计算一下这张试卷的分数
            int totalScore = 0;
            List<TestQuestion> testQuestions = testQuestionMapper.selectByPaperIdWithQuestionType(testQuestion.getPaperId());
            for (TestQuestion question : testQuestions) {
                totalScore += question.getScore();
            }
            TestPaper testPaper = testPaperMapper.selectByPrimaryKey(testQuestion.getPaperId());
            testPaper.setMaxScore(totalScore);
            //修改这张试卷的分数
            int row = testPaperMapper.updateByPrimaryKey(testPaper);
            if (!(rows == 1 && row == 1)) {
                throw new Exception();
            }
            Db.commitTransaction();
        } catch (Exception e) {
            e.printStackTrace();
            Db.rollbackTransaction();
            return Result.fail("修改失败");
        } finally {
            Db.close();
        }
        return Result.okMsg("修改成功");
    }

    @Override
    public Result insert(TestQuestion testQuestion) {
        try {
            Db.startTransaction();
            int rows = testQuestionMapper.insert(testQuestion);
            TestPaper testPaper = testPaperMapper.selectByPrimaryKey(testQuestion.getPaperId());
            //让试卷总分加上添加的题目的分数
            testPaper.setMaxScore(testPaper.getMaxScore() + testQuestion.getScore());
            int row = testPaperMapper.updateByPrimaryKey(testPaper);
            if (!(rows == 1 && row == 1)) {
                throw new Exception();
            }
            Db.commitTransaction();
        } catch (Exception e) {
            e.printStackTrace();
            Db.rollbackTransaction();
        } finally {
            Db.close();
        }
        return Result.okMsg("添加成功");
    }

    @Override
    public Result delete(int id) {
        try {
            Db.startTransaction();
            TestQuestion oldTestQuestion = testQuestionMapper.selectByPrimaryKey(id);
            if (oldTestQuestion == null) {
                return Result.fail("该题目不存在");
            }
            int rows = testQuestionMapper.deleteByPrimaryKey(id);
            TestPaper testPaper = testPaperMapper.selectByPrimaryKey(oldTestQuestion.getPaperId());
            //让试卷总分减去删除题目的分数
            testPaper.setMaxScore(testPaper.getMaxScore() - oldTestQuestion.getScore());
            int row = testPaperMapper.updateByPrimaryKey(testPaper);
            if (!(rows == 1 && row == 1)) {
                throw new Exception();
            }
            Db.commitTransaction();
        } catch (Exception e) {
            e.printStackTrace();
            Db.rollbackTransaction();
        } finally {
            Db.close();
        }
        return Result.okMsg("删除成功");
    }
}
